const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    more: true,
    moveData1: null,
    moveData2: null,
    inputVal: "",
    inputVal2: "搜索",
    openUrl: ['/pages/ruku/newRuku/newRuku','1','2'],
    page:1,
    supList:[],
    key:"",
    isHideLoadMore: true,
    rukupage: false,
    propage: false
  },
  changeText: function (e) {
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail && e.detail.value
    this.setData(nameMap)
  },
  clickScreen: function () {
    if (this.data.masking) {
      this.setData({
        masking: false,
        image: '../images/screen.png'
      })
    } else {
      this.setData({
        masking: true,
        image: '../images/screen-active.png'
      })
    }
  },
  sltScn: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.id ? e.currentTarget.dataset.id : null
    console.log(id)
    this.setData({
      page: 1,
      houseid: id
    })
    this.getList()
  },
  moreClick: function () {
    console.log(this.data.more)
    var animation1 = wx.createAnimation({
      duration: 3000,
      delay: 0,
      timingFunction: "ease",
    })
    var animation2 = wx.createAnimation({
      duration: 3000,
      delay: 0,
      timingFunction: "ease",
    })
    if (this.data.more) {
      animation1.translate(-55, 0).step({ duration: 1000 })
      animation2.translate(0, -55).step({ duration: 1000 })
    } else {
      animation1.translate(0, 0).step({ duration: 1000 })
      animation2.translate(0, 0).step({ duration: 1000 })
    }
    this.setData({
      more: !this.data.more,
      moveData1: animation1.export(),
      moveData2: animation2.export()
    })
  },
  search: function () {
    this.setData({
      page: 1
    })
    this.getList()
  },
  open: function (e) {
    var id = e.currentTarget.dataset.id
    var that=this
    var barr = []
    console.log(this.data.propage)
    if(this.data.propage){
      barr = ['预览', '修改', '选择供应商']
    }else{
      barr = ['预览', '修改', '删除供应商']
    }
    wx.showActionSheet({
      itemList: barr,
      success: function (res) {
        console.log(res)
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            wx.navigateTo({
              url: './addSupplier/addSupplier?create=1&&id=' + id
            })
          }else if(res.tapIndex==1){
            wx.navigateTo({
              url: './addSupplier/addSupplier?id=' + id
            })
          } else if (res.tapIndex == 2) {
            if (that.data.rukupage || that.data.propage) {
              let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
              let prevPage = pages[pages.length - 2];
              var obj = e.currentTarget.dataset.item
              prevPage.setData({
                ProviderId: id,
                ProviderName: obj.ProviderName
              })
              wx.navigateBack({
                delta: 1
              })
            } else {
              wx.showModal({
                title: '删除产品',
                content: '你确定删除该产品吗？',
                confirmText: "删除",
                cancelText: "取消",
                success: function (res) {
                  console.log(res);
                  if (res.confirm) {
                    console.log('删除')
                    app.req({
                      url: 'User/Provider/' + id,
                      method: 'DELETE',
                      success: function (res) {
                        if (res.data.Code) {
                          wx.showToast({
                            title: '删除成功',
                            icon: 'success',
                            duration: 1500
                          });
                          wx.startPullDownRefresh()
                        } else {
                          wx.showToast({
                            title: '删除失败',
                            icon: 'none',
                            duration: 1500
                          });
                        }
                      }
                    })
                  } else {
                    console.log('取消')
                  }
                }
              })
            }
          }
        }
      }
    });
  },
  getList: function(){
    console.log(1)
    var that=this
    app.req({
      url: 'User/Provider',
      data: {
        pageindex: this.data.page,
        pagesize: 15,
        key: this.data.key
      },
      success: function(res){
        // if (res.data.Result.length > 0) {
          if(that.data.page==1){
            that.setData({
              supList: res.data.Result
            })
          }else if(that.data.page>1){
            that.setData({
              supList: that.data.supList.concat(res.data.Result)
            })
          }
        // }
        if (res.data.Result.length == 0){
          that.setData({
            page: that.data.page - 1
          })
        }
        setTimeout(function () {
          wx.stopPullDownRefresh()
        }, 1000)
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.type == 2){
      this.setData({
        rukupage: true
      })
    }else if(options.type==4){
      this.setData({
        propage: true
      })
    }
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (a,b,c) {
    // this.onPullDownRefresh()
    wx.startPullDownRefresh({

    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      key: ''
    })
    this.getList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('加载更多')
    this.setData({
      isHideLoadMore: false,
    })
    this.setData({
      page: this.data.page + 1
    })
    this.getList()
    setTimeout(() => {
      this.setData({
        isHideLoadMore: true,
      })
    }, 1000)
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})